package model

import "time"

type RawMaterial struct {
	Id int64 `xorm:"pk autoincr"`
	Type int
	PurchaseOrderId int64
	Name string
	Company string
	SpecificationUnit string
	SpecificationAndQuantity int64
	RetailPrice float64
	Stock int64
	PurchaseQuantity int
	Total float64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type TemporaryType struct {
	RawMaterialId int
	RawMaterialList []*RawMaterial
}

type PurchaseOrder struct {
	Id int64 `xorm:"pk autoincr"`
	TemporaryTypeList []*TemporaryType `xorm:"-"`
	OrderAmount float64
	PreferentialAmount float64
	ActualDifference float64
	AmountPayable float64
	OfferDescription string
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (r *RawMaterial)DistinguishingTypes(rs []*RawMaterial) ([]*TemporaryType, error){
	var rawMaterialTypeList []*TemporaryType
	for _, rv := range rs{
		var judge bool = false
		for _, rmtilv := range rawMaterialTypeList{
			if rv.Type == rmtilv.RawMaterialId{
				judge = true
				rmtilv.RawMaterialList = append(rmtilv.RawMaterialList, rv)
			}
		}
		if judge == false{
			var tt TemporaryType
			tt.RawMaterialId = rv.Type
			tt.RawMaterialList = append(tt.RawMaterialList, rv)
			rawMaterialTypeList = append(rawMaterialTypeList, &tt)
		}
	}

	return rawMaterialTypeList, nil
}

func (po *PurchaseOrder) QueryPurchaseOrde(poId int64)(*PurchaseOrder, error){
	var pov PurchaseOrder
	var rs []*RawMaterial
	x.Where("purchase_order_id").Find(&rs)

	r := new(RawMaterial)
	temporaryTypeList, err := r.DistinguishingTypes(rs)
	if err != nil{
		return nil, err
	}
	pov.TemporaryTypeList = temporaryTypeList

	return &pov, nil
}